﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Managers.IManagers;
using GameEngine.Entities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace GameEngine.EntityComponents
{
    public class VortexComponent : IEntityComponent
    {
        private bool timeJump = false;
        private bool jumping = false;
        private AbstractEntity entity;
        private string timeJumpSound = "Sound/vortex";

        public VortexComponent(AbstractEntity entity)
        {
            this.entity = entity;
            try
            {
                SoundEffect soundEffect = entity.game.Content.Load<SoundEffect>(timeJumpSound);
                ((IAudioManager)(entity.game.Services.GetService(typeof(IAudioManager)))).AddSoundEffect(soundEffect, timeJumpSound);
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Updates the component
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public void Update(GameTime gameTime)
        {
            timeJump = false;
        }

        public string TimeJumpSound
        {
            get
            {
                return timeJumpSound;
            }
            set
            {
                timeJumpSound = value;
                SoundEffect soundEffect = entity.game.Content.Load<SoundEffect>(timeJumpSound);
                ((IAudioManager)(entity.game.Services.GetService(typeof(IAudioManager)))).AddSoundEffect(soundEffect, timeJumpSound);
            }
        }


        /// <summary>
        /// Gets or sets a value indicating whether [time jump].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [time jump]; otherwise, <c>false</c>.
        /// </value>
        public bool TimeJump
        {
            get { return timeJump; }
            set { timeJump = value; }
        }

        /// <summary>
        /// This method decides what to do if a collision occurs with this component
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="isX">if set to <c>true</c> [is X].</param>
        /// <param name="isY">if set to <c>true</c> [is Y].</param>
        public void CollideWith(Entities.AbstractEntity target, bool isX, bool isY)
        {
            MoveableComponent mComp = (MoveableComponent)target.GetComponent(typeof(MoveableComponent));
            if (mComp != null && mComp.SpecialAction1 && !jumping)
            {
                jumping = true;
                timeJump = true;
                ((IAudioManager)(entity.game.Services.GetService(typeof(IAudioManager)))).PlaySound(timeJumpSound, 1.0f);
                
            }
            if (jumping && !mComp.SpecialAction1)
            {
                jumping = false;
            }
        }
    }
}
